//app.js
App({
    onLaunch: function () {
    },
    globalData: {
        userInfo: null,
        version: "1.0",
        shopName: "Python3 + Flask 订餐全栈系统",
        domain: "http://192.168.1.228:5000/api"
    },
    tip: function (params) {
        var that = this;
        var title = params.hasOwnProperty('title') ? params['title'] : '提示信息';
        var content = params.hasOwnProperty('content') ? params['content'] : '';
        wx.showModal({
            title: title,
            content: content,
            success: function (res) {

                if (res.confirm) { //点击确定
                    if (params.hasOwnProperty('cb_confirm') && typeof(params.cb_confirm) == "function") {
                        params.cb_confirm();
                    }
                } else { //点击否
                    if (params.hasOwnProperty('cb_cancel') && typeof(params.cb_cancel) == "function") {
                        params.cb_cancel();
                    }
                }
            }
        })
    },
    alert: function (params) {
        var title = params.hasOwnProperty('title') ? params['title'] : '提示信息';
        var content = params.hasOwnProperty('content') ? params['content'] : '';
        wx.showModal({
            title: title,
            content: content,
            showCancel: false,
            success: function (res) {
                if (res.confirm) { //用户点击确定
                    if (params.hasOwnProperty('cb_confirm') && typeof(params.cb_confirm) == "function") {
                        params.cb_confirm();
                    }
                } else {
                    if (params.hasOwnProperty('cb_cancel') && typeof(params.cb_cancel) == "function") {
                        params.cb_cancel();
                    }
                }
            }
        })
    },
    showLoading: function () {
        wx.showLoading('请稍等..');
    },
    hideLoading: function () {
        wx.hideLoading();
    },
    console: function (msg) {
        console.log(msg);
    },
    getRequestHeader: function () {
        return {
            'content-type': 'application/x-www-form-urlencoded',
            'Authorization': this.getCache('token')
        }
    },
    buildUrl: function (path, params) {
        var url = this.globalData.domain + path;
        var paramUrl = url;
        if (params) {
            paramUrl = Object.keys(params).map(function (value) {
                return [encodeURIComponent(value), encodeURIComponent(params[value])].join('=')
            }).join('&');

            paramUrl = '?' + paramUrl
        }
        return paramUrl;
    },
    getCache: function (key) {
        var value = undefined;
        try {
            value = wx.getStorageSync(key);
        }
        catch (e) {

        }
        return value;
    },
    setCache: function (key, value) {
        wx.setStorage({
            'key': key,
            'data': value
        });
    },
    //获取当前页
    getCurrentPageUrl: function () {
        var pages = getCurrentPages();   //获取加载的页面
        var currentPage = pages[pages.length - 1];    //获取当前页面的对象
        var url = currentPage.route;    //当前页面url
        return url;
    },
    //获取当前页带参数的url
    getCurrentPageUrlWithArgs: function () {
        var pages = getCurrentPages();    //获取加载的页面
        var currentPage = pages[pages.length - 1];   //获取当前页面的对象
        var url = currentPage.route;   //当前页面url
        var options = currentPage.options;   //如果要获取url中所带的参数可以查看options

        //拼接url的参数
        var urlWithArgs = url + '?';
        for (var key in options) {
            var value = options[key];
            urlWithArgs += key + '=' + value + '&'
        }
        urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1);
        return urlWithArgs;
    }

});